package com.ruoyi.web.controller.app.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.sj.domain.PersonalData;
import com.ruoyi.sj.mapper.PersonalDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PersonalDataService
 * @Description TODO
 * @Author tyc@yanyu
 * @Date 2018/12/10 0010 09:39
 **/
@Service
public class AppPersonalService {
    @Autowired
    private PersonalDataMapper personalDataMapper;

    /**
     * 新增个人资料
     * @param map
     * @return
     */
    public Object addPersonal(Map<String, String> map) {
        PersonalData personalData = new PersonalData();
        personalData.setBackgroundPicture(map.get("backgroundPicture")); //背景图片
        personalData.setBirthday(map.get("birthday")); //生日
        if (StringUtils.isBlank(map.get("memberId"))||"null".equals(map.get("memberId"))){
            throw new XException("用户id不能为空");
        }
        personalData.setMemberId(Long.valueOf(map.get("memberId"))); //用户id
        personalData.setHeight(map.get("height")); //身高
        personalData.setNickName(map.get("nickName")); //昵称
        if (StringUtils.isBlank(map.get("sex"))||"null".equals(map.get("sex"))){
            personalData.setSex(3); // 性别不明
        }else {
            personalData.setSex(Integer.valueOf(map.get("sex")));//性别
        }
        personalData.setWeight(map.get("weight")); //体重
        personalData.setDeclaration(map.get("declaration")); //交友宣言
        personalData.setObjective(map.get("objective")); //交友目的
        personalData.setOccupation(map.get("objective")); //职业
        personalData.setHobby(map.get("hobby")); //爱好
        personalData.setSpeciality(map.get("speciality")); //特长
        personalData.setLoveConcept(map.get("loveConcept"));//恋爱观念
        personalData.setFirstTimeHope(map.get("firstTimeHope"));//首次见面希望
        personalData.setLikePlace(map.get("likePlace")); //喜欢相约的地点
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        personalData.setRecentOnLineTime(sdf.format(new Date())); //最近上线时间

        return personalDataMapper.insertPersonalData(personalData);
    }

    /**
     * 编辑资料
     * @param map
     * @return
     */
    public Object updatePersonal(Map<String, String> map) {
        PersonalData personalData = new PersonalData();
        personalData.setBackgroundPicture(map.get("backgroundPicture")); //背景图片
        personalData.setBirthday(map.get("birthday")); //生日
        if (StringUtils.isBlank(map.get("memberId"))||"null".equals(map.get("memberId"))){
            throw new XException("用户id不能为空");
        }
        personalData.setMemberId(Long.valueOf(map.get("memberId"))); //用户id
        personalData.setHeight(map.get("height")); //身高
        personalData.setNickName(map.get("nickName")); //昵称
        if (StringUtils.isBlank(map.get("sex"))||"null".equals(map.get("sex"))){
            personalData.setSex(3); // 性别不明
        }else {
            personalData.setSex(Integer.valueOf(map.get("sex")));//性别
        }
        personalData.setWeight(map.get("weight")); //体重
        personalData.setDeclaration(map.get("declaration")); //交友宣言
        personalData.setObjective(map.get("objective")); //交友目的
        personalData.setOccupation(map.get("objective")); //职业
        personalData.setHobby(map.get("hobby")); //爱好
        personalData.setSpeciality(map.get("speciality")); //特长
        personalData.setLoveConcept(map.get("loveConcept"));//恋爱观念
        personalData.setFirstTimeHope(map.get("firstTimeHope"));//首次见面希望
        personalData.setLikePlace(map.get("likePlace")); //喜欢相约的地点
        personalData.setRecentOnLineTime(map.get("recentOnLineTime")); //最近上线时间

        return personalDataMapper.updatePersonalByMemberId(personalData);
    }

    /**
     * 获取用户闺蜜圈资料
     * @param memberId
     * @return
     */
    public Object getPersonalList(String memberId) {
        if (StringUtils.isBlank(memberId)){
            throw new XException("用户ID不能为空");
        }
        return personalDataMapper.selectPersonalDataById(Long.valueOf(memberId));
    }

    /**
     * 修改上线时间
     * @param memberId
     * @return
     */
    public Object updateOnLineTime(String memberId) {
        if (StringUtils.isBlank(memberId)){
            throw new XException("memberId不能为空");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PersonalData personalData = new PersonalData();
        personalData.setRecentOnLineTime(sdf.format(new Date()));
        personalData.setMemberId(Long.valueOf(memberId));
        //登录时修改闺蜜圈最近上线时间
        return personalDataMapper.updatePersonalByMemberId(personalData);
    }
}
